package com.simple.datax.flow.processor;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.flow.constants.ProcessResultConstants;
import com.simple.datax.flow.constants.TaskActivityConstants;
import com.simple.datax.flow.convertor.Convertor;
import com.simple.datax.flow.dao.ibatis.model.FlowActivityInstance;
import com.simple.datax.flow.generator.MessageGenerator;
import com.simple.datax.flow.message.BaseEcMessage;
import com.simple.datax.flow.message.EcFlowMessage;
import com.simple.datax.flow.message.EcFlowRespMessage;
import com.simple.datax.flow.preprocessor.PreProcessorAdaptor;
import com.simple.datax.flow.preprocessor.PreProcessorService;
import com.simple.datax.flow.service.TaskActivityManager;
import com.simple.datax.flow.service.TaskManager;
import com.simple.datax.flow.service.TaskMessageManager;
import com.simple.datax.flow.util.FailMessageUtil;

/** 
 * @Title: EcFlow201MessageProcess.java 
 * @Package com.simple.datax.flow.processor 
 * @Description: TODO(ECFlow201Message报文处理) 
 * @author tonney 
 * @date 2013-3-4 下午06:48:40 
 * @version V1.0 
 */
public class EcFlow201MessageProcessor extends BaseMessageProcessor implements MessageProcessIF{
	public Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public int process(BaseEcMessage message) {
		List<BaseEcMessage> messageList = null;
		PreProcessorService preProcessor = null;
		EcFlowMessage orgFlowMessage = null; // 接收到的未处理的原始报文，用于出现错误后原始报文返回
		int result = ProcessResultConstants.STATUS_SUCCESS;
		EcFlowMessage ecFlowMessage = null;
		String xml = "";
		try {
			ecFlowMessage = (EcFlowMessage)message;
			orgFlowMessage = ecFlowMessage;
			/****************************** 报文校验 BEGIN ***********************************************/
			result = messageConvertor.xsdValidate(message.getOrgXmlCode());
			/****************************** 报文校验 END **************************************************/
			if (result == ProcessResultConstants.STATUS_SUCCESS){
				List<String> xmlMessageList = new ArrayList<String>();
				List<FlowActivityInstance> activityInstanceList = new ArrayList<FlowActivityInstance>();
				/***************************** 协议企业接收后预处理 BEGIN ****************************************/
				messageList = new ArrayList<BaseEcMessage>();
				preProcessor = preProcessorAdaptor
						.getPreProcessorServiceByEcode(ecFlowMessage
								.getFlowMessageHead().getSenderId());
				if (preProcessor != null) {
					result = preProcessor.recvPreProcessor(ecFlowMessage,
							messageList); // 预处理报文
				} else {
					messageList.add(ecFlowMessage);
				}
				/***************************** 协议企业接收后预处理 END ********************************************/
	
				if (result == ProcessResultConstants.STATUS_SUCCESS) {
					result = processActivitys(messageList, xmlMessageList, activityInstanceList);
					if(result == ProcessResultConstants.STATUS_SUCCESS){
						result = updateFlowActivitys(activityInstanceList);
						if(result == ProcessResultConstants.STATUS_SUCCESS){
							result = sendXmlMessages(xmlMessageList);
							if(result == ProcessResultConstants.STATUS_SUCCESS){
								log.debug("EcFlow201MessageProcessor::process Send Message to router==>taskid["
										+ orgFlowMessage.getFlowMessageBody().getTaskId()
										+ "],msgid["
										+ orgFlowMessage.getFlowMessageBody().getMessageId()
										+ "],result[" + result+ "],resultInfo[" + ProcessResultConstants.getProcDetail(result)+"]");
							}
						}
					}
				}
			}
		} catch (Exception e) {
			// 如果执行到此步,说明有异常,则返回错误报文给发送者
			result = ProcessResultConstants.STATUS_PROC_EXCEPTION;
			log.error("EcFlow201MessageProcess::process error==>" + e.toString());
		} finally {
			// 并且原始报文解析成功，但是报文处理过程中异常，则发送错误响应报文
			if (result != ProcessResultConstants.STATUS_SUCCESS
					&& orgFlowMessage != null) {
				sendFailMessages(orgFlowMessage,result);
			}
		}
		return result;
	}
}
